package com.tan.androidtest.customview.ondrawbase;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.tan.androidtest.R;

/**
 * 最基本的自定义view
 */

public class DemoView1 extends View {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public DemoView1(Context context) {
        super(context);
    }

    public DemoView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DemoView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);//都加上最好,因为如果是自定义TextView这样的,设置了它的text属性的话,不掉用super方法可能会导致text不显示

        //画底色
        //canvas.drawColor(ContextCompat.getColor(getContext(), R.color.blue_1aacf7));//一般用于在绘制之前设置底色，或者在绘制之后为界面设置半透明蒙版

        //画圆
        //canvas.drawCircle(500, 500, 100, mPaint);

        //画点
        // 2个为一组
        //float[] points = {0, 0, 50, 50, 100, 100, 500, 500};
        //canvas.drawPoints(points, mPaint);//画点的时候应该先设置paint的strokeWidth大小

        //画椭圆
        //canvas.drawOval(0, 0, 400, 500, mPaint);

        //画直线
        //canvas.drawLine(0, 0, 600, 600, mPaint);

        //画圆角矩形
        //left, top, right, bottom 是四条边的坐标，rx 和 ry 是圆角的横向半径和纵向半径。
        //canvas.drawRoundRect(0, 0, 400, 400, 50, 50, mPaint);

        //画弧形或者扇形
        // drawArc() 是使用一个椭圆来描述弧形的。left, top, right, bottom 描述的是这个弧形所在的椭圆；
        // startAngle 是弧形的起始角度（x 轴的正向，即正右的方向，是 0 度的位置；
        // 顺时针为正角度，逆时针为负角度），sweepAngle 是弧形划过的角度；
        // useCenter 表示是否连接到圆心，如果不连接到圆心，就是弧形，如果连接到圆心，就是扇形。
        //mPaint.setStyle(Paint.Style.STROKE);
        //canvas.drawArc(0, 0, 400, 300, 100, 180, true, mPaint);


        //画一个饼

        //1.画红色的扇形 画线和字
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.red_ff2e31));
        canvas.drawArc(30, 30, 430, 430, 180, 118, true, mPaint);
        mPaint.setStrokeWidth(3);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.white));
        mPaint.setTextAlign(Paint.Align.RIGHT);
        mPaint.setTextSize(25);
        float[] result = getTipLineCoordinate(180, 118);
        canvas.drawLine(result[0], result[1], result[0] - 10, result[1] - 10, mPaint);
        canvas.drawLine(result[0] - 10, result[1] - 10, result[0] - 60, result[1] - 10, mPaint);
        canvas.drawText("Lollipop", result[0] - 70, result[1] - 8, mPaint);


        //画黄色的扇形
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.yellow_febc44));
        canvas.drawArc(50, 50, 450, 450, 300, 60, true, mPaint);

        //画深紫色的扇形
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.purple_9726B2));
        canvas.drawArc(50, 50, 450, 450, 5, 24, true, mPaint);

        //画灰色
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.gray_888888));
        canvas.drawArc(50, 50, 450, 450, 30, 10, true, mPaint);

        //画绿色
        mPaint.setColor(Color.parseColor("#309788"));
        canvas.drawArc(50, 50, 450, 450, 42, 60, true, mPaint);

        //画蓝色
        mPaint.setColor(Color.parseColor("#4096F7"));
        canvas.drawArc(50, 50, 450, 450, 105, 75, true, mPaint);

    }

    /**
     * 获取指示线的坐标
     */
    private float[] getTipLineCoordinate(int startAngle, int sweepAngle) {
        int degree = sweepAngle / 2;
        //获得对边长
        double sinLength = 200 * Math.sin(Math.PI * degree / 180.0);
        //Y 坐标 = 半径 - 对边长 + 起点有30
        float y = 230f - (float) sinLength;

        //X 坐标 = 半径 - 斜边长  + 起点有30
        double cosLength = 200 * Math.cos(Math.PI * degree / 180.0);
        float x = 230f - (float) cosLength;

        float[] result = {x, y};
        return result;
    }


}
